import request from '@/utils/http'
import type { BaseResult, PageResult } from '@/types/axios'
import type { ConfigItem, ConfigQuery } from '@/api/model/configModel'

export class ConfigService {
  static getAll(params: ConfigQuery) {
    return request.post<BaseResult<PageResult<ConfigItem[]>>>({
      url: '/config/getAll',
      params
    })
  }

  static save(params: Omit<ConfigItem, 'configId' | 'createBy' | 'createTime' | 'updateBy' | 'updateTime'>) {
    return request.post<BaseResult>({
      url: '/config/save',
      params
    })
  }

  static update(params: ConfigItem) {
    return request.post<BaseResult>({
      url: '/config/update',
      params
    })
  }

  static delete(configId: number) {
    return request.post<BaseResult>({
      url: '/config/delete',
      params: { configId }
    })
  }

  static getById(configId: number) {
    return request.post<BaseResult<ConfigItem>>({
      url: '/config/getById',
      params: { configId }
    })
  }

  /**
   * 全局获取单个配置参数值
   */
  static async getValue(configKey: string): Promise<string | null> {
    const res = await request.post<BaseResult<string>>({
      url: '/config/getValue',
      params: { configKey }
    })
    if (String(res.code) === '200') {
      return res.data ?? null
    }
    return null
  }
}


